package me.project.jobster.service;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.GUIDGenerator;
import me.project.jobster.common.util.MD5Utils;
import me.project.jobster.common.util.Response;
import me.project.jobster.dao.mybatis.mapper.CompanyMapper;
import me.project.jobster.dao.mybatis.mapper.StudentMapper;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Mcdull
 * @date 2018-4-26
 */
@Service
public class LoginService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CompanyMapper companyMapper;

    /**
     * student register
     *
     * @param name
     * @param email
     * @param password
     * @return
     */
    public Response studentRegister(String name, String email, String password) {
        Response response = Response.getInstance("", "");
        Student student = new Student();
        student.setEmail(email);
        //check the student register or not
        int count = studentMapper.selectCount(student);
        if (count > 0) {
            logger.info("register a student  failed: Email has already existed!");
            response.setRespMessage("Email has already existed!");
            return response;
        }
        //save account
        try {
            student.setName(name);
            student.setGpaStatus(GlobalConstants.GPA_FLAG_INVISIBLE);
            student.setsId(GUIDGenerator.getInstance().generateUID());
            student.setPassword(MD5Utils.md5Bit32Upper(password));
            studentMapper.insert(student);
            response.setSuccess(true);
        } catch (Exception e) {
            response.setRespMessage("Register failed!");
            e.printStackTrace();
            logger.info("register a student  failed: insert  failed ! exception: {}", e.getMessage());
        }
        return response;
    }

    /**
     * student login
     *
     * @param email
     * @param password
     * @return
     */
    public Response studentLogin(String email, String password) {
        Response response = Response.getInstance("", "");
        Student student = new Student();
        student.setEmail(email);
        student.setPassword(MD5Utils.md5Bit32Upper(password));
        Student s = studentMapper.selectOne(student);
        if (s == null) {
            logger.info("login failed: Email or Password is incorrect!");
            response.setRespMessage("Email or Password is incorrect!");
            return response;
        }
        response.setResult(s);
        response.setSuccess(true);
        return response;
    }

    /**
     *
     * @param name
     * @param email
     * @param password
     * @return
     */
    public Response companyRegister(String name, String email, String password) {
        Response response = Response.getInstance("", "");
        Company company = new Company();
        company.setEmail(email);
        //check the company register or not
        int count = companyMapper.selectCount(company);
        if (count > 0) {
            logger.info("register a company  failed: Email has already existed!");
            response.setRespMessage("Email has already existed!");
            return response;
        }
        //save account
        try {
            company.setName(name);
            company.setcId(GUIDGenerator.getInstance().generateUID());
            company.setPassword(MD5Utils.md5Bit32Upper(password));
            companyMapper.insert(company);
            response.setSuccess(true);
        } catch (Exception e) {
            response.setRespMessage("Register failed!");
            e.printStackTrace();
            logger.info("register a company  failed: insert  failed ! exception: {}", e.getMessage());
        }
        return response;
    }

    /**
     * student login
     *
     * @param email
     * @param password
     * @return
     */
    public Response companyLogin(String email, String password) {
        Response response = Response.getInstance("", "");
        Company company = new Company();
        company.setEmail(email);
        company.setPassword(MD5Utils.md5Bit32Upper(password));
        Company c = companyMapper.selectOne(company);
        if (c == null) {
            logger.info("login failed: Email or Password is incorrect!");
            response.setRespMessage("Email or Password is incorrect!");
            return response;
        }
        response.setResult(c);
        response.setSuccess(true);
        return response;
    }
}
